//
//  ActivityIndicatorCoverView.m
//  Project
//
//  Created by Suny on 14-11-25.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "ActivityIndicatorCoverView.h"
#import "UIView+BaseFramework.h"
// failRetryImage
#define Image_FailRetry_Width   236
#define Image_FailRetry_Height  161

@interface ActivityIndicatorCoverView ()
{
    SEL      retryAction;
    id       actObject;
    UIButton *retryButton;
    NSString *_title;


    UILabel *retryTitleLabel;
}

@end

@implementation ActivityIndicatorCoverView

@synthesize indicator;

- (id)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame
                         title:nil
                     imageName:nil];
}

// sinaLoadingLogo.png //


- (id)initWithFrame:(CGRect)frame
              title:(NSString *)title
          imageName:(NSString *)name
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor     = [UIColor clearColor];
        self.autoresizesSubviews = YES;
        UIImage *image = nil;
        indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        if (name)
        {
            image = [UIImage imageNamed:name];
        }
        else
        {
//            image     = [UIImage imageNamed:@"common_cover_loading_logo.png"];
        }

        CGPoint centerP = CGPointMake(ceilf(self.bounds.size.width / 2), ceilf(self.bounds.size.height / 2));

        loadLogoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        loadLogoImage.center           = centerP;
        loadLogoImage.image            = image;
        loadLogoImage.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
        [self addSubview:loadLogoImage];

		indicator.centerX = self.width /2;
		indicator.centerY = self.height /2;
        if (image == nil)
        {
            indicator.center = centerP;
        }
        indicator.hidesWhenStopped = YES;
        indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
        [self addSubview:indicator];

        _title = [title copy];
        CGFloat y    = CGRectGetMaxY(loadLogoImage.frame) + 10;
        CGRect  rect = CGRectMake(10, y, 200, 30);
        titleLabel = [[UILabel alloc] initWithFrame:rect];
        titleLabel.backgroundColor  = [UIColor clearColor];
        titleLabel.font             = [UIFont systemFontOfSize:24];
        titleLabel.textColor        = [UIColor blackColor];
        titleLabel.textAlignment    = NSTextAlignmentRight;
        titleLabel.text             = _title;
        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
        [self addSubview:titleLabel];

        // retryButton
        retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect clickRect = CGRectMake(0, 0, Image_FailRetry_Width, Image_FailRetry_Height);
        retryButton.frame  = clickRect;
		retryButton.centerY = loadLogoImage.width == 0 ? self.height/2 : loadLogoImage.bottom + 10;
		retryButton.centerX = self.width/2;
        [retryButton addTarget:self
                        action:@selector(tapped)
              forControlEvents:UIControlEventTouchUpInside];
        [retryButton setImage:[UIImage imageNamed:@"WebView_LoadFail_Refresh_Icon"]
                     forState:UIControlStateNormal];
        [retryButton setImage:[UIImage imageNamed:@"WebView_LoadFail_Refresh_Icon"]
                     forState:UIControlStateHighlighted];
        retryButton.hidden           = YES;

		retryTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,retryButton.bottom - 50,
                                                                             120,
                                                                             40)];
        retryTitleLabel.text = @"点击刷新";
        retryTitleLabel.backgroundColor = [UIColor clearColor];
		retryTitleLabel.textColor = [UIColor lightGrayColor];
        retryTitleLabel.font = [UIFont systemFontOfSize:18];
		retryTitleLabel.textAlignment = NSTextAlignmentCenter;
		retryTitleLabel.centerX = self.width/2.0;
        [self addSubview:retryTitleLabel];


        retryButton.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
        [self addSubview:retryButton];

        self.hidden = YES;
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    CGPoint centerP = CGPointMake(ceilf(self.bounds.size.width / 2), ceilf(self.bounds.size.height / 2));
    loadLogoImage.center = centerP;

	indicator.centerX = self.width /2;
	indicator.centerY = loadLogoImage.height == 0 ? self.height /2:loadLogoImage.bottom +10;
//    CGFloat y    = CGRectGetMaxY(loadLogoImage.frame) - 15;
//    CGRect  rect = CGRectMake(indicator.frame.origin.x - 280, y, 260, 30);
//    titleLabel.frame = rect;
}

- (void)startAnimating
{
    titleLabel.hidden    = NO;
    loadLogoImage.hidden = NO;
    retryButton.hidden   = YES;
    retryTitleLabel.hidden = YES;
    [indicator startAnimating];

    self.hidden = NO;
}

- (void)startAnimatingWithTitle:(NSString *)str
{
    titleLabel.hidden = NO;
    titleLabel.text   = str;
    [self startAnimating];
}

- (void)stopAnimating
{
    titleLabel.hidden    = YES;
    loadLogoImage.hidden = YES;
    [indicator stopAnimating];
    self.hidden = YES;
}

- (void)stopAnimationWithRetryAction:(SEL)aRetryAction withActObject:(id)object;
{

    self.hidden          = NO;
    titleLabel.hidden    = YES;
    loadLogoImage.hidden = YES;
    [indicator stopAnimating];
    retryButton.hidden = NO;
    retryTitleLabel.hidden = NO;

	retryAction = aRetryAction;
    actObject   = object;

}

@end


@implementation ActivityIndicatorCoverView (Action)

- (void)tapped
{
    [self startAnimatingWithTitle:_title];
    if ([actObject respondsToSelector:retryAction])
    {
        [actObject performSelector:retryAction];
    }
}


@end